package com.excuse.excusebuilder.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.commen.util.ToastUtil;
import com.excuse.excusebuilder.impl.internet.CheckInternetService;
import com.excuse.excusebuilder.model.Tngou;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by U-nookia on 2017/4/2.
 * 检测网络状态的广播接收器
 */

public class InternetConnectStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo activeNetwork = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())
                ||activeNetwork == null
                || !activeNetwork.isConnected()) { // connected to the internet
            NetWorkUtil.netWorkConnected = false;
            ToastUtil.makeShortToast(context,"网络断开，请检查网络连接");
            return;
        }

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
            if (activeNetwork.isConnected()){
                NetWorkUtil.netWorkConnected = true;
                ToastUtil.makeShortToast(context,"网络已连接");
                return;
            }
        }
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            if (activeNetwork.isConnected()){
                initChectInternetRetrofit(context);
            }else {
                NetWorkUtil.netWorkConnected = false;
                ToastUtil.makeShortToast(context,"网络断开，请检查网络连接");
            }
        }
    }

    private void initChectInternetRetrofit(final Context context) {
        String url = "http://www.tngou.net";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheckInternetService service = retrofit.create(CheckInternetService.class);
        Call<Tngou> call = service.getList("cook",0, 1, 20);
        call.enqueue(new Callback<Tngou>() {
            @Override
            public void onResponse(Call<Tngou> call, Response<Tngou> response) {
                if (response.isSuccessful()){
                    NetWorkUtil.netWorkConnected = true;
                    ToastUtil.makeShortToast(context,"网络已连接");
                }else {
                    NetWorkUtil.netWorkConnected = false;
                    ToastUtil.makeShortToast(context,"网络无法使用");
                }
            }

            @Override
            public void onFailure(Call<Tngou> call, Throwable t) {
                NetWorkUtil.netWorkConnected = false;
                ToastUtil.makeShortToast(context,"网络无法使用");
            }
        });
    }
}
